import os
import requests


server = "http://127.0.0.1:8000"


class GameContent:
    def __init__(self):
        self.checkerboard = []

    def start_game(self):
        url = os.path.join(server, "play/tictactoe/start")
        response = requests.post(url, data={})
        print(response.status_code)
        self.checkerboard = response.json()["checkerboard"]

    def play_game(self):
        self.print_checkerboard()
        input_num = int(input("Please input the position index you want to put: ")) - 1
        print(input_num)
        url = os.path.join(server, "play/tictactoe")
        response = requests.post(url, data={"input_num": input_num})

    def print_checkerboard(self):
        char_dict = {
            0: "[1]",
            1: "[2]",
            2: "[3]",
            3: "[4]",
            4: "[5]",
            5: "[6]",
            6: "[7]",
            7: "[8]",
            8: "[9]",
        }
        for i in range(9):
            if self.checkerboard[i] == 0:
                target_char = char_dict[i]
            elif self.checkerboard[i] == 1:
                target_char = "[X]"
            elif self.checkerboard[i] == 2:
                target_char = "[O]"
            else:
                target_char = ""
                print("Server error!")
            print(target_char, end="")
            if (i + 1) % 3 == 0:
                print("")
            else:
                print(",", end="")


def main():
    content = GameContent()
    content.start_game()
    content.play_game()


if __name__ == "__main__":
    main()
